package com.taotao.content.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.content.jedis.JedisClient;
import com.taotao.content.service.ContentService;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;

@Service
public class ContentServiceImpl implements ContentService{
	
	@Autowired
	private JedisClient client;

	@Autowired
	private TbContentMapper tcMapper;
	
	@Value("${CONTENT_KEY}")
	private String CONTENT_KEY;
	
	
	@Override
	public TaotaoResult saveContent(TbContent tbContent) {
		//1.注入mapper
		//2.不全属性
		tbContent.setCreated(new Date());
		tbContent.setUpdated(tbContent.getCreated());
		//3.保存
		tcMapper.insertSelective(tbContent);
		//当添加内容的时候记得删除缓存
		try {
			System.out.println("缓存已删除");
			client.hdel(CONTENT_KEY, tbContent.getCategoryId().toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}


	@Override
	public List<TbContent> getContentListByCatgoryId(Long categoryId) {
		// 1.注入mapper
		//2.创建example
		
		try {
			//判断redis中是否有数据
			String jsonData = client.hget(CONTENT_KEY, categoryId+"");
			
			if(StringUtils.isNoneBlank(jsonData)) {
				System.out.println("这里已有缓存");
				return JsonUtils.jsonToList(jsonData, TbContent.class);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
		TbContentExample tbContentExample = new TbContentExample();
		Criteria criteria = tbContentExample.createCriteria();
		//3.设置查询的条件
		criteria.andCategoryIdEqualTo(categoryId);
		//4.执行查询
		List<TbContent> list = tcMapper.selectByExample(tbContentExample);
		
		//将数据写入到redis中
		//调用方法写入redis key value
		try {
			System.out.println("这里没有缓存");
			client.hset(CONTENT_KEY, categoryId+"", JsonUtils.objectToJson(list));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return list;
	}
	
	@Override
	public EasyUIDataGridResult getContentByCategoryIdList(Long categoryId,Integer page, Integer rows) {
		//1.设置分页信息
		if(page == null) page =1;
		if(rows == null) page =1;
		PageHelper.startPage(page, rows);
		//2.注入mapper
		//3.创建example对象，。不需要设置对象
		TbContentExample example = new TbContentExample(); 
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		//根据mapper调用查询方法
		List<TbContent> list = tcMapper.selectByExample(example);
		//4.获取到分页信息
		PageInfo<TbContent> pInfo = new PageInfo<>(list);
		//分装到EasyUIDataGridResult
		EasyUIDataGridResult eResult = new EasyUIDataGridResult();
		eResult.setTotal((int) pInfo.getTotal());
		eResult.setRows(pInfo.getList());
		return eResult;
	}

}
